<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RegulateController extends Controller
{
    //

    /**
     * @Author <winner443@163.com>
     *
     * @return Response
     */
    public function regulate()
    {
        /*
         * @Author <winner>
         * function 管理员控制器
         * */
        //读取管理员数据
        $data = \DB::select("select a.*,(select s.group_name from sys_group as s where s.id = a.auth_id) as group_name from admins as a");
        return view('admin.regulate')->with('data',$data);
    }


    //添加管理员页面
    public function regulateAdd(){
        $group_name = \DB::table('sys_group')->get();
        return view('admin.adminAdd')->with(['group_name'=>$group_name]);
    }
    //添加管理员操作
    public function regulateCreate(Request $request){
        //接收全部数据
        $values = $request->all();
        //设置验证规则
        $rule = [
            'login_name'=>'required|unique:admins,login_name|regex:/^[a-zA-Z][a-zA-Z0-9_]{3,10}$/',
            //'login_name'=>array('regex:/^[a-zA-Z][a-zA-Z0-9_]{4,10}$/'),//TODO <winner remarks>正则如果要是这样写，那么required就不能验证了。
            'pwd'=>'required|min:6|max:18|same:repwd',
            'tel'=>'required|regex:/^1[34578]\d{9}$/',
            'email'=>'required|email',
            'auth_id'=>'required'
        ];
        //设置提示信息
        $message = [
            'login_name.required'=>'用户名不能为空',
            'login_name.unique'=>'用户名已存在，请重新输入',
            'login_name.regex'=>'用户名格式必须为字母、数字、下划线，3-10位组成。',
            'pwd.max'=>'请输入6-18位密码',
            'pwd.min'=>'请输入6-18位密码',
            'pwd.required'=>'密码不能为空',
            'pwd.same'=>'两次密码不一致',
            'tel.required'=>'手机号不能为空',
            'tel.regex'=>'手机号格式不正确',
            'email.required'=>'邮箱不能为空',
            'email'=>'邮箱格式不正确',
            'auth_id.required'=>'角色不能为空'
        ];
        $validator = \Validator::make($values,$rule,$message);
        if ($validator->passes()){
            //插入数据
            //过滤掉多余的字段
            unset($_POST['repwd']);
            //$_POST['pwd'] = base64_encode(md5($_POST['pwd']));
            $_POST['pwd'] = md5(SALT.$_POST['pwd']);
            $_POST['token'] = str_random(50);
            $_POST['create_time'] = time();
            $_POST['update_time'] = null;
            $result = \DB::table('admins')->insert($_POST);
            if ($result){
                $data = [
                    'status'=>'200',
                    'href'=>'http://mfwx.bjmfzyz.com/admin/regulate',
                    'info'=> '添加成功'
                ];
                return json_encode($data);
            }else{
                $data = [
                    'status'=>'202',
                    'info'=>'添加失败，稍后重试'
                ];
                return json_encode($data);
            }
        }else{
            return response()->json([
                'status'=>'201',
                'info'=>$validator->errors()->first()
            ]);
        }
    }
    //编辑管理员
    public function regulateEdit($id,Request $request){
        if ($request->isMethod('GET')){
            //角色列表
            $group_name = \DB::table('sys_group')->get();
            //查询管理员，嵌套查询看此管理员属于哪一个角色的。
            $data = \DB::select("select a.*,(select s.group_name from sys_group as s where s.id = a.auth_id) as group_name from admins as a where a.id = $id");
            return view('admin.adminEdit')->with(['data'=>$data,'group_name'=>$group_name]);
        }else{
            //接收全部数据
            $values = $request->all();
            //设置验证规则
            $rule = [
                'login_name'=>'required|regex:/^[a-zA-Z][a-zA-Z0-9_]{3,10}$/|unique:admins,login_name,'.$id,//忽略指定此id的验证
                'tel'=>'required|regex:/^1[34578]\d{9}$/',
                'email'=>'required|email',
            ];
            $message = [
                'login_name.required'=>'用户名不能为空',
                'login_name.unique'=>'用户名已存在，请重新输入',
                'login_name.regex'=>'用户名格式必须为字母、数字、下划线，3-10位组成。',
                'tel.required'=>'手机号不能为空',
                'tel.regex'=>'手机号格式不正确',
                'email.required'=>'邮箱不能为空',
                'email'=>'邮箱格式不正确'
            ];
            $validator = \Validator::make($values,$rule,$message);
            if ($validator->passes()){
                $_POST['update_time'] = time();
                $result = \DB::table('admins')->where('id','=',$id)->update($_POST);
                if ($result){
                    $data = [
                        'status'=>'200',
                        'info'=>'修改成功'
                    ];
                    return json_encode($data);
                }else{
                    $data = [
                        'status'=>'202',
                        'info'=>'修改失败'
                    ];
                    return json_encode($data);
                }
            }else{
                return response()->json([
                    'status'=>'201',
                    'info'=>$validator->errors()->first()
                ]);
            }
        }
    }
    //停用管理员
    public function regulateStop($id,Request $request){
        if ($request->input('start')=='1'){
            //停用
            $data = [];
            $data['start'] = 1;
            $result = \DB::table('admins')->where('id','=',$id)->update($data);
            if ($result){
                $data = [
                    'status'=>'200',
                    'info'=>'停用成功'
                ];
                return json_encode($data);
            }else{
                $data = [
                    'status'=>'201',
                    'info'=>'停用失败'
                ];
                return json_encode($data);
            }
        }else{
            //启用
            $data['start'] = 0;
            $result = \DB::table('admins')->where('id','=',$id)->update($data);
            if ($result){
                $data = [
                    'status'=>'200',
                    'info'=>'启用成功'
                ];
                return json_encode($data);
            }else{
                $data = [
                    'status'=>'201',
                    'info'=>'启用失败'
                ];
                return json_encode($data);
            }
        }
    }
    //修改管理员密码
    public function regulateResetPwd($id,Request $request){
        if ($request->isMethod('GET')){
            $data = \DB::select("select a.*,(select s.group_name from sys_group as s where s.id = a.auth_id) as group_name from admins as a where a.id = $id");
            return view('admin.adminResetPwd')->with(['data'=>$data]);
        }else{
            $values = $request->all();
            $rule = [
                'pwd'=>'required|min:5|max:18|same:repwd',
            ];
            $message = [
                'pwd.required'=>'密码不能为空',
                'pwd.min'=>'请输入6-18位密码',
                'pwd.max'=>'请输入6-18位密码',
                'pwd.same'=>'两次密码不一致'
            ];
            $validator = \Validator::make($values,$rule,$message);
            if ($validator->passes()){
                $data = array();
                $data['pwd'] = md5(SALT.$request->input('pwd'));
                $result = DB::table('admins')->where('id','=',$id)->update($data);
                if ($result){
                    $data = [
                        'status'=>'10000',
                        'info'=>'密码修改成功'
                    ];
                    return $data;
                }else{
                    $data = [
                        'status'=>'10004',
                        'info'=>'密码修改失败'
                    ];
                    return $data;
                }
            }else{
                return response()->json([
                    'status'=>'10001',
                    'info'=>$validator->errors()->first()
                ]);
            }
        }
    }



}
